package com.ellricky.baselib.widget;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.andr.common.util.EmptyUtil;
import com.ellricky.baselib.R;
import com.ellricky.baselib.adapter.PersonItemDecoration;
import com.ellricky.baselib.adapter.WorkUploadAdapter;
import com.ellricky.baselib.adapter.WorkUploadItemDecoration;
import com.ellricky.baselib.listener.OnTakePhotoListener;
import com.ellricky.baselib.listener.WorkUploadListener;
import com.ellricky.baselib.util.EDialogUtil;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;


public class WorkUploadView extends FrameLayout implements AdapterView.OnItemClickListener {

    private Activity activity;

    private RecyclerView rvPic;
    private WorkUploadAdapter adapter;

    private List<String> picUrlList;

    private WorkUploadListener listener;

    public void setListener(WorkUploadListener listener) {
        this.listener = listener;
    }

    public WorkUploadView(Context context) {
        super(context);
        initView(context);
    }

    public WorkUploadView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public WorkUploadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }


    private void initView(Context context) {
        activity = (Activity) context;

        View.inflate(context, R.layout.base_layout_work_upload, this);
        rvPic = findViewById(R.id.rvPic);

        GridLayoutManager glm = new GridLayoutManager(context, 4) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };

        rvPic.setLayoutManager(glm);
        rvPic.addItemDecoration(new WorkUploadItemDecoration(activity, 10));
        rvPic.setNestedScrollingEnabled(false);

        adapter = new WorkUploadAdapter(activity);
        adapter.setOnItemClickListener(this);
        rvPic.setAdapter(adapter);
    }


    public void updateAdapterData(String picUrl, int position) {
        if (picUrlList == null)
            picUrlList = new ArrayList<>();

        picUrlList.add(position, picUrl);
        adapter.refresh(picUrlList);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
        if (EmptyUtil.isEmpty(adapter.get(position))) {
            // 添加图片
            EDialogUtil.showTakePhotoDialog(activity, new OnTakePhotoListener() {
                @Override
                public void onClickItem(DialogInterface dialogInterface, int item) {
                    dialogInterface.dismiss();
                    if (listener != null) listener.takePhoto(position, item);
                }
            });

        } /*else {
            // 已添加的图片,点击是否可查看大图
        }*/
    }


    public String getUrls() {
        StringBuilder sb = new StringBuilder();
        if (picUrlList != null && picUrlList.size() > 0) {
            for (String url : picUrlList) {
                sb.append(url).append(";");
            }
            sb.deleteCharAt(sb.length() - 1);
        }

        String urls = sb.toString();
        Logger.t("uploadView").e("%s", urls);

        return urls;
    }


}
